package com.bestcem.xm.member.mq.receiver;//package com.bestcem.xm.member.mq.receiver;
//
//import com.alibaba.fastjson.JSON;
//import com.bestcem.xm.member.mq.message.FootprintMessage;
//import com.bestcem.xm.member.mq.message.OrgCreatedMessage;
//import com.bestcem.xm.member.mq.message.UserMessage;
//import com.bestcem.xm.member.service.MemberMqHandler;
//import com.bestcem.xm.util.MsgPackFormatUtil;
//import com.rabbitmq.client.Channel;
//import lombok.extern.slf4j.Slf4j;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.util.Base64;
//
///**
// * @author ming.xue <ming.xue@idiaoyan.com>
// * @desc 联系人通用消息接收
// * @date 2021-10-29
// */
//@Slf4j
//@Component("memberCommonReceiver")
//public class MemberCommonReceiver {
//    private static final Logger EMAIL_LOG = LoggerFactory.getLogger("EmailErrorLog");
//
//    /**
//     * 联系人消息处理器
//     */
//    @Autowired
//    private MemberMqHandler memberMqHandler;
//
//    /**
//     *
//     * @param message
//     * @param channel
//     * @throws IOException
//     */
//    @RabbitListener(queues = "#{appMemberCommonQueue.name}", ackMode = "MANUAL",
//            concurrency = "${app.member.queue.common.receiver-consume-concurrency:100}")
//    public void receive(Message message, Channel channel) throws IOException {
//        long deliveryTag = message.getMessageProperties().getDeliveryTag();
//        try {
//            MsgPackFormatUtil.MqMessage mqMessage = MsgPackFormatUtil.mqDecode(message.getBody());
//            if (mqMessage == null) {
//                EMAIL_LOG.error("【联系人】消息解析失败: {}", Base64.getEncoder().encodeToString(message.getBody()));
//                channel.basicNack(deliveryTag, false, false);
//                return;
//            }
//            if (log.isInfoEnabled()) {
//                log.info("app消费mq消息收到消息:{}", JSON.toJSONString(mqMessage));
//            }
//
//            if ("user.user_created".equals(mqMessage.getTopic())) {
//                UserMessage userMessage = MsgPackFormatUtil.mapToObj(mqMessage.getData(), UserMessage.class);
//                memberMqHandler.createInnerMemberByCreateUser(userMessage);
//            }
//            if ("user.user_updated".equals(mqMessage.getTopic())) {
//                UserMessage userMessage = MsgPackFormatUtil.mapToObj(mqMessage.getData(), UserMessage.class);
//                memberMqHandler.updateInnerMemberByUpdateUser(userMessage);
//            }
//            if ("user.org_created".equals(mqMessage.getTopic())) {
//                OrgCreatedMessage msg = MsgPackFormatUtil.mapToObj(mqMessage.getData(), OrgCreatedMessage.class);
//                // 创建默认标签分组及标签
//                memberMqHandler.createDefaultLabelGroup(msg);
//                // 创建联系人可见范围
//                memberMqHandler.createMemberDisplayRange(msg);
//            }
//            if ("member.footprint_save".equals(mqMessage.getTopic())) {
//                FootprintMessage footprintMessage = MsgPackFormatUtil.mapToObj(mqMessage.getData(), FootprintMessage.class);
//                memberMqHandler.saveFootprint(footprintMessage);
//            }
//
//            channel.basicAck(deliveryTag, false);
//        } catch (Exception e) {
//            EMAIL_LOG.error("【联系人】处理倍市得app的mq消息异常:{}",
//                    Base64.getEncoder().encodeToString(message.getBody()), e);
//            try {
//                channel.basicNack(deliveryTag, false, false);
//            } catch (Exception e2) {
//                EMAIL_LOG.error("【联系人】处理倍市得app的mq消息, nack mq消息异常: {}",
//                        Base64.getEncoder().encodeToString(message.getBody()), e2);
//            }
//        }
//    }
//}
